﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SuperStarProject.Models;
using System.Data.SqlClient;
using SuperStarProject.Models;

namespace SuperStarProject.DAO
{
    public class PhieuMuaHangDAO
    {
        public static List<PhieuMuaHangModel> ShowAllMyPhieuMuaHang(string username)
        {
            List<PhieuMuaHangModel> prod = new List<PhieuMuaHangModel>();

            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@makh", (object)username));

            SqlDataReader rdr = DataProvider.RunStoredProcParams_Read("sp_select_PhieuMuaHang_ByMaKH_All", db,lstParameter);
            try
            {
                return Convert(rdr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<PhieuMuaHangModel> ShowAllMyPhieuMuaHangDaiLy(string madaily, string tinhtranggiao)
        {
            List<PhieuMuaHangModel> prod = new List<PhieuMuaHangModel>();

            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@madaily", (object)madaily));
            lstParameter.Add(new SqlParameter("@tinhtranggiao", (object)tinhtranggiao));

            SqlDataReader rdr = DataProvider.RunStoredProcParams_Read("sp_select_PhieuMuaHang_ByMaDaiLy_TinhTrang", db, lstParameter);
            try
            {
                return Convert(rdr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static PhieuMuaHangModel GetDetails(string maphieumuahang)
        {
            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@ma", (object)maphieumuahang));

            SqlDataReader rdr = DataProvider.RunStoredProcParams_Read("sp_select_PhieuMuaHang_dk", db, lstParameter);
            try 
            { 
                return Convert(rdr).First();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public static int Create(PhieuMuaHangModel item)
        {
            SqlConnection db = DataProvider.DBconnection();
            
            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@maphieumua", (object)item.MaPhieuMuaHang));
            lstParameter.Add(new SqlParameter("@makh", (object)item.MaKH));
            lstParameter.Add(new SqlParameter("@masp", (object)item.MaSP));
            lstParameter.Add(new SqlParameter("@madaily", (object)item.MaDaiLy));
            lstParameter.Add(new SqlParameter("@soluongmua", (object)item.SoLuongMua));
            try
            {
                return DataProvider.RunStoredProcParams_NoRead("sp_muahang", db, lstParameter);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        static List<PhieuMuaHangModel> Convert(SqlDataReader rdr)
        {
            List<PhieuMuaHangModel> lstItem = new List<PhieuMuaHangModel>();
            while (rdr.Read())
            {
                PhieuMuaHangModel item = new PhieuMuaHangModel();
                item.MaPhieuMuaHang = (string)rdr["MaPhieuMuaHang"];
                item.MaKH = (string)rdr["MaKH"];
                item.MaSP = (string)rdr["MaSP"];
                item.MaDaiLy = (string)rdr["MaDaiLy"];
                item.TongDiemThuong = (int)rdr["TongDiemThuong"];
                // Cho nay phai goi ham de lay tong diem thuong da su dung
                item.TongDiemThuongDaSuDung = PhieuChonQuaTangDAO.TongDiemThuongDaSuDung(item.MaPhieuMuaHang);
                item.NgayMua = (DateTime)rdr["NgayMua"];
                item.SoLuongMua = (int)rdr["SoLuongMua"];
                item.TinhTrangGiao = (string)rdr["TinhTrangGiao"];
                if (rdr["NgayNhan"].ToString()!= "")
                    item.NgayNhan = (DateTime)rdr["NgayNhan"];
                item.TongTien = (int)rdr["TongTien"];
             
                lstItem.Add(item);
            }
            return lstItem;
        }
        public static int Edit(PhieuMuaHangModel item)
        {
            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@maphieu", (object)item.MaPhieuMuaHang));
            lstParameter.Add(new SqlParameter("@sl", (object)item.SoLuongMua));
            lstParameter.Add(new SqlParameter("@dl", (object)item.MaDaiLy));
            try
            {
                return DataProvider.RunStoredProcParams_NoRead("upd_phieumuahang", db, lstParameter);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        
        }
        public static int EditState(PhieuMuaHangModel item)
        {
            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@maphieu", (object)item.MaPhieuMuaHang));
            lstParameter.Add(new SqlParameter("@sl", (object)item.SoLuongMua));
            lstParameter.Add(new SqlParameter("@dl", (object)item.MaDaiLy));
            try
            {
                return DataProvider.RunStoredProcParams_NoRead("upd_phieumuahang", db, lstParameter);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static int Delete(PhieuMuaHangModel item)
        {
            SqlConnection db = DataProvider.DBconnection();
            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@maphieumua", (object)item.MaPhieuMuaHang));
            try
            {
                return DataProvider.RunStoredProcParams_NoRead("sp_huyhang", db, lstParameter);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static bool ChangeStateWithNgayNhan(string maphieumuahang)
        {
            SqlConnection db = DataProvider.DBconnection();
            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@maphieumuahang", (object)maphieumuahang));
            try
            {
                DataProvider.RunStoredProcParams_NoRead("upd_phieumuahang_danhan", db, lstParameter);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public static bool ChangeState(string maphieumuahang,string tinhtranggiao)
        {
            SqlConnection db = DataProvider.DBconnection();
            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@maphieumuahang", (object)maphieumuahang));
            lstParameter.Add(new SqlParameter("@tinhtranggiao", (object)maphieumuahang));
            try
            {
                DataProvider.RunStoredProcParams_NoRead("upd_phieumuahang_tinhtrang", db, lstParameter);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
